<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * @param {number[]} nums
         * @return {number[][]}
         * 思路 双指针 循环i l = i + 1,r = length - 1 每次循环双指针 注意去重 i !== i - 1  
         */
        var threeSum = function (nums) {
            nums.sort((a, b) => a - b);
            let len = nums.length, result = [];
            if (len < 3) return [];
            for (let i = 0; i < len - 2; i++) {
                if (nums[i] > 0) break;
                if (i > 0 && nums[i] === nums[i - 1]) continue;
                let l = i + 1, r = len - 1;
                while (l < r) {
                    const num = nums[i] + nums[l] + nums[r];
                    if (num < 0) { l++; continue; }
                    if (num > 0) { r--; continue; }
                    result.push([nums[i], nums[l], nums[r]]);
                    while (l < r && nums[l] === nums[++l]);
                    while (l < r && nums[r] === nums[--r]);
                }
            }
            return result;
        };
    </script>
</body>

</html>